package com.onlygx.dva.common.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.onlygx.dva.common.properties.CommonSwaggerProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger 配置文件
 *
 * @author : GaoXiang
 * 时间 : 2021/12/10
 * 介绍 : Swagger2 配置文件
 */
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@RequiredArgsConstructor
public class Swagger2Config {

    final CommonSwaggerProperties commonSwaggerProperties;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(commonSwaggerProperties.getEnabled())
                .groupName(commonSwaggerProperties.getApiGroupName())
                .useDefaultResponseMessages(false)
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage(commonSwaggerProperties.getApiBasePackage()))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(commonSwaggerProperties.getTitle())
                .description(commonSwaggerProperties.getDescription())
                .contact(new Contact("api文档提供", "", ""))
                .version("2.x")
                .build();

    }

}

